﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DataModel;
using System.Collections.ObjectModel;
using System.Linq;
using A_ManageUsers.Services.Security;
namespace A_ManageUsers.DataModels
{
    public class Role : DataModelBase
    {
        Services.Security.Role R;
        public Role(Services.Security.Role R)
        {
            this.R = R;
            if (R.Abilities == null)
            {
                R.Abilities = new int[0];
            }
            RoleAbilities = RoleAbility.GetRolesAbilities(R.ID, R.Abilities);
            if (IsNew)
            {
                foreach (var item in RoleAbilities)
                {
                    item.NewObject();
                }
            }
            Abilities.AbilitiesL.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(AbilitiesL_CollectionChanged);
        }



        #region Basic Properties
        public int ID { get { return R.ID; } }
        public string Name
        {
            get { return R.Name; }
            set
            {
                if (R.Name != value)
                {
                    base.SetRevertValue("Name", R.Name);
                    R.Name = value;
                    OnPropertyChanged("Name");
                    if (value == "")
                        AddMessage(new ErrorMessage("Name", "You must enter a name for the ability", null));
                    else
                        RemoveMessage(new ErrorMessage("Name", "You must enter a name for the ability", null));
                }
            }
        }
        public string Notes
        {
            get { return R.Notes; }
            set
            {
                if (R.Notes != value)
                {
                    base.SetRevertValue("Notes", R.Notes);
                    R.Notes = value;
                    OnPropertyChanged("Notes");
                }
            }
        }
        #endregion

        #region RoleAbilities
        public ObservableCollection<RoleAbility> RoleAbilities { get; set; }
        void AbilitiesL_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (e.OldItems != null)
            {
                foreach (Ability item in e.OldItems)
                {
                    var Removal = RoleAbilities.First((RA) => RA.AbilityID == item.ID);
                    RoleAbilities.Remove(Removal);
                }
            }
            if (e.NewItems != null)
            {
                foreach (Ability item in e.NewItems)
                {
                    RoleAbilities.Add(new RoleAbility(R.ID, R.Abilities.Contains(item.ID), item));
                }
            }
        }
        #endregion

        protected override void PropertyChangedMethod(string PropertyName, object PropertyValue)
        {
            base.PropertyChangedMethod(PropertyName, PropertyValue);
            base.SetComitAction(PropertyName, PropertyValue, CommitChange);
        }

        void CommitChange(DataModelBase DMB, string PropertyName, object Value)
        {
            UMC.UpdateRolePropetyAsync(((Role)DMB).R.ID, PropertyName, Value);
        }

        protected override void ComitNewObject()
        {
            R.Abilities = RoleAbilities.Where((RA) => RA.Enabled).Select((RA) => RA.AbilityID).ToArray();
            UMC.NewRoleCompleted += new EventHandler<Services.Security.NewRoleCompletedEventArgs>(UMC_NewRoleCompleted);
            UMC.NewRoleAsync(R);
            Roles.RolesL.Add(this);
        }

        void UMC_NewRoleCompleted(object sender, Services.Security.NewRoleCompletedEventArgs e)
        {
            R.ID = e.Result;
        }

        UserManagementClient UMC = ServiceConfig.NewUserManagementClient();

        public override string ToString()
        {
            return Name;
        }
    }

    public static class Roles
    {
        static Roles()
        {
            UserManagementClient UMC = ServiceConfig.NewUserManagementClient();


            UMC.GetRolesCompleted += (s, ea) =>
                {
                    foreach (var item in ea.Result)
                    {
                        RolesL.Add(new Role(item));
                    }
                };
            UMC.GetRolesAsync();
        }
        public static ObservableCollection<Role> RolesL = new ObservableCollection<Role>();
    }
}
